/*
modbus_485.h
offer modbus_485.c var and function extern
designed by lunanting

---------------------------
2020-03-24 11:09:40
create file
---------------------------
*/
#ifndef __MODBUS_485_H
#define __MODBUS_485_H

#include "aos/mbmaster.h"
#include "aos/kernel.h"


//modbus queue buffer size
#define MD_QUEUE_SIZE 20
//modbus write buffer size
#define MD_WRITEBUF_SIZE 10

//modbus function define
#define MODBUS_HOLD_W 0
#define MODBUS_HOLD_R 1
#define MODBUS_COIL_W 2
#define MODBUS_COIL_R 3


/*****************type define*****************/
//buzy state
typedef enum{
    mb_free=0,  //modbus free
    mb_buzy     //modbus buzy
}MB_buzy_state;

//modbus function
typedef enum{
    mb_hold_w=0,    //modbus hold write 0x10||0x06
    mb_hold_r,      //modbus hold read 0x03
    mb_coil_w,      //modbus coil write 0x05 || 0x0f
    mb_coil_r,      //modbus coil read 0x01
    mb_single_w,
    mb_single_r,
    mb_common_w,    //modbus common write
    mb_common_r     //modbus common read
}MB_fun_type;

//
//modbus operation data
typedef struct{
    MB_fun_type mb_fun; //modbus function
    uint8_t slave_id; //device id
    uint8_t fun_code;   //function code
    uint16_t start_addr;    //register address start
    uint16_t rwLen;   //read/write length
    uint16_t *wBuf;  //write buffer
    uint8_t *resLen;   //response length
    uint8_t *resBuf;    //response buffer
}MB_data;

//modbus state
typedef struct{
    MB_data data;   //operation state
    MB_buzy_state buzy_state; //bus buzy state,0=free,1=buzy
    mb_handler_t *mb_handler;   //modbus handler
    uint16_t write_buf[MD_WRITEBUF_SIZE]; //write buffer
    uint8_t port;  //uart port
    int speed; //uart speed
    uint8_t init;   //init state
    mb_parity_t parity;  //uart parity
    aos_sem_t sem;  //single thread
    int wait_time;  //wait time
}MB_state;



/**********************extern function*****************/
extern MB_buzy_state xp_get_modbus_buzy(uint8_t port);  //get modbus buzy state
extern int xp_modbus_read(uint8_t port,const MB_fun_type fun,const uint8_t slave_id,const uint16_t start_addr,const uint16_t read_len,uint8_t *resp_buf,uint16_t *resp_len);    //modbus read data
extern int xp_modbus_write(uint8_t port,const MB_fun_type fun,const uint8_t slave_id,const uint16_t start_addr,uint16_t wLen,const uint16_t *wBuf,uint8_t *resBuf,uint16_t *resLen);    //modbus write data
extern int xp_modbus_read_MB(uint8_t port,MB_data *data);    //modbus read with MB_data
extern int xp_modbus_write_MB(uint8_t port,MB_data *data);   //modbus write with MB_data
extern int xp_modbus_init(const uint8_t port,const int speed,const mb_parity_t parity,const int  wait_time);   //init a modbus on a uart
extern int xp_modbus_Deinit(uint8_t port);  //modbus port deinit
extern int xp_modbus_ask(uint8_t port,MB_data *data);   //modbus master ask slave device
extern int xp_modbus_transfer(MB_state *state); //modbus exchange data

extern void xp_modbus_test(uint8_t id,uint16_t addr,uint16_t val);  //modbus hold set

#endif
//end of the file
